<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-6-3
 * Func   后台栏目管理    
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table     = Table('admin_menu');
$page      = 'admin/menu';
if (IS_POST){
    $operation = NoBadStr($_REQUEST['operation']);
    $flag      = '';
    $data = array(
        'name' => NoBadStr($_REQUEST['name']),
        'pid'  => NoBadStr($_REQUEST['pid']),
        'm'    => NoBadStr($_REQUEST['set_m']),
        'c'    => NoBadStr($_REQUEST['set_c']),
        'a'    => NoBadStr($_REQUEST['set_a']),
        'sort' => NoBadStr($_REQUEST['sort']) ? NoBadStr($_REQUEST['sort']) : 0,
        'hide' => NoBadStr($_REQUEST['hide']) ? NoBadStr($_REQUEST['hide']) : 0,
        'son'  => NoBadStr($_REQUEST['son']) ? NoBadStr($_REQUEST['son']) : 0,
    );
    switch ($operation){
        case 'add':
            $id = NoBadStr($_REQUEST['id']);
            if ($id){
                // 修改
                $execSql = 'UPDATE ' . $table . ' SET ' . CreateUpdateSql($data) . ' WHERE id = ' . $id;
            }else{
                $execSql = 'INSERT INTO ' . $table . ' SET ' . CreateUpdateSql($data);
            }
            $flag   = $db->exec($execSql);
            break;
    }
    if ($flag){
        ShowMsg('操作成功',U('admin/menu'));
    }else{
        ShowMsg('操作失败', -1);
    }
}


// 载入所有的menu，并且设置好树形结构
$menuTreeData    = getMenuTree($db);
switch ($a){
    case 'add':
        // 是否设置子级
        $id      = NoBadStr($_REQUEST['id']);
        $page    = 'admin/menu_add';
        break;
    case 'edit':
        $id      = NoBadStr($_REQUEST['id']);
        $infoSql = 'SELECT * FROM ' . $table . ' WHERE id = ' . $id;
        $info    = $db->getrow($infoSql);
        $page    = 'admin/menu_edit';
        break;
    case 'close':
        $id      = NoBadStr($_REQUEST['id']);
        if (!$id) {
            ShowMsg('操作失败', -1);
        }
        $udpData = array('hide' => 1);
        $execSql = 'UPDATE ' . $table . ' SET ' . CreateUpdateSql($udpData) . ' WHERE id = ' . $id;
        $flag    = $db->exec($execSql);
        if ($flag){
            ShowMsg('操作成功', U('admin/menu'));
        }else{
            ShowMsg('操作失败', -1);
        }
        break;
    case 'open':
        $id      = NoBadStr($_REQUEST['id']);
        if (!$id){
            ShowMsg('操作失败', -1);
        }
        $udpData = array('hide' => 0);
        $execSql = 'UPDATE ' . $table . ' SET ' . CreateUpdateSql($udpData) . ' WHERE id = ' . $id;
        $flag    = $db->exec($execSql);
        if ($flag){
            ShowMsg('操作成功', U('admin/menu'));
        }else{
            ShowMsg('操作失败', -1);
        }
        break;
    default :
        break;
}
$tpl->display($page);


function getMenuTree($db){
    // 查询所有的栏目信息，
    $sqlGetMenu = 'SELECT * FROM ' . Table('admin_menu') . ' ORDER BY id ASC';
    $menuList   = $db->getall($sqlGetMenu);
    $menuIdKey  = array();
    foreach ($menuList as $mk => $mv){
        $menuIdKey[$mv['id']] = $mv;
    }
    // menu信息准备生成树状结构
    // 递归获取级联数据
    $arr              = array();
    foreach ($menuList as $k => $v){
        if ($v['pid'] == 0){
            $v['lv'] = '1';
            $arr[]   = $v;
            getChild($menuList, $v['id'], $arr, 1);
        }
    }
    return $arr;
}

function getChild($menuList, $id, &$arr, $lv){
    $lv++;
    foreach ($menuList as $k => $v){
        if ($v['pid'] == $id){
            $v['lv']   = $lv;
            $line      = '';
            for($i = 1; $i < $lv; $i++){
                $line  = $line . '－－';
            }
            $v['line'] = $line;
            $arr[]     = $v;
            // 检查是否还有子项
            getChild($menuList, $v['id'], $arr, $lv);
        }
    }
}